﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;
using System.Drawing;
using ESRI.ArcGIS.Controls;
using PlanningGIS.Util.Management;
using ESRI.ArcGIS.Display;
using PlanningGIS.Win.Controls;
using System.IO;
using PlanningGIS.Util.IO;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Symbol
{
    /// <summary>
    /// 色带下拉框
    /// </summary>
    public partial class ComboBoxColorRamp : ComboBox
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ComboBoxColorRamp()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            DrawMode = DrawMode.OwnerDrawFixed;
            DropDownStyle = ComboBoxStyle.DropDownList;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="container"></param>
        public ComboBoxColorRamp(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            DrawMode = DrawMode.OwnerDrawFixed;
            DropDownStyle = ComboBoxStyle.DropDownList;
        }

        /// <summary>
        /// 获取当前的符号集合
        /// </summary>
        public Dictionary<int, IStyleGalleryItem> Symbols { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public string ColorScheme
        {
            get;
            set;
        }

        /// <summary>
        /// 当前选择的色带
        /// </summary>
        public IColorRamp SelectedColorRamp
        {
            get
            {
                if (this.SelectedIndex < 0) return null;
                IStyleGalleryItem itm = Symbols[this.SelectedIndex];
                return itm.Item as IColorRamp;
            }
            set
            {
                if (Symbols == null || Symbols.Count == 0) return;
                KeyValuePair<int, IStyleGalleryItem> itm = Symbols.FirstOrDefault(t => (t.Value.Item as IColorRamp).Name.Equals(value.Name, StringComparison.CurrentCultureIgnoreCase));
                if (itm.Value == null) return;
                this.SelectedIndex = itm.Key;
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            this.Items.Clear();
            string stylefile = AoHelper.StylePath + "\\ESRI.ServerStyle";
            if (!File.Exists(stylefile)) return;
            try
            {
                ISymbologyControl SymbologyCreator = new SymbologyControl() as ISymbologyControl;
                SymbologyCreator.LoadStyleFile(stylefile);
                ISymbologyStyleClass symbologyStyleClass = SymbologyCreator.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);
                int count = symbologyStyleClass.get_ItemCount(symbologyStyleClass.StyleCategory);
                Symbols = new Dictionary<int, IStyleGalleryItem>();
                for (int i = 0; i < count; i++)
                {
                    IStyleGalleryItem itm = symbologyStyleClass.GetItem(i);
                    Symbols.Add(i, itm);
                    stdole.IPictureDisp picture = symbologyStyleClass.PreviewItem(symbologyStyleClass.GetItem(i), Width, Height);
                    Image image = Image.FromHbitmap(new IntPtr(picture.Handle));
                    Items.Add(image);
                }
                if (Items.Count > 0) SelectedIndex = 0;
                SymbologyCreator.RemoveFile(stylefile);
                //GC.Collect();
            }
            catch(Exception ex)
            {
                MsgHelper.ShowErrorMsgbox("加载符号文件失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 重绘子项
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            if (e.Index < 0) return;
            try
            {
                //显示图片
                Image image = (Image)Items[e.Index];
                System.Drawing.Rectangle rect = e.Bounds;
                e.Graphics.DrawImage(image, rect);
            }
            catch
            {

            }
            finally
            {
                base.OnDrawItem(e);
            }
        }
    }
}
